<div class="page page-full bg-white animation-fade">
    <div class="page-aside">
        <div class="page-aside-switch">
            <i class="icon wb-chevron-left" aria-hidden="true"></i>
            <i class="icon wb-chevron-right" aria-hidden="true"></i>
        </div>
        <div class="page-aside-inner height-full" data-toggle="slimScroll">
            <div class="page-aside-section">
                <h5 class="page-aside-title">表名</h5>
                <div class="list-group" id="allTable_info" data-toggle="ListGroup">
                    <script id="allTables" type="text/html">
                        {{each　allTables }}
                        <a class="list-group-item" href="javascript:;"
                           onclick="Page.CodeGenerator.entityGenerator('{{$index}}')">
                            {{$index}}: {{$value}}
                        </a>
                        {{/each}}
                    </script>
                </div>
            </div>
        </div>
    </div>
    <div class="page-main">
        <div class="page-header">
            <ul class="nav nav-tabs" style=" margin-left:10px;margin-bottom:0px;">
                <li><a href="#" onclick="Page.CodeGenerator.changeEidtor('entityCode')">Entity</a></li>
                <li><a href="#" onclick="Page.CodeGenerator.changeEidtor('controllerCode')">Controller</a></li>
                <li><a href="#" onclick="Page.CodeGenerator.changeEidtor('serviceCode')">Service</a></li>
                <li><a href="#" onclick="Page.CodeGenerator.changeEidtor('repositoryCode')">Repository</a></li>
                <li><a href="#" onclick="Page.CodeGenerator.changeEidtor('dtoCode')">Dto</a></li>
                <li><a href="#" onclick="Page.CodeGenerator.changeEidtor('converterCode')">Converter</a></li>
            </ul>
        </div>
        <div class="page-content ">
            <textarea id="code" realName="code"></textarea>
        </div>
    </div>

    <div class="site-action">

        <button type="button" class="site-action-toggle btn-raised btn btn-success btn-floating">
            <i class="fa fa-wrench animation-scale-up" data-toggle="tooltip" data-placement="left" title="操作"></i>
            <i class="fa fa-close animation-scale-up hidden" data-toggle="tooltip" data-placement="left" title="关闭"></i>
        </button>
        <div class="site-action-buttons">
            <button type="button" id="import" class="btn-raised btn btn-success btn-floating " data-toggle="tooltip"
                    data-placement="left" title="导入">
                <i class="fa fa-external-link" aria-hidden="true"></i>
            </button>
            <button type="button" id="import_current" class="btn-raised btn btn-success btn-floating "
                    data-toggle="tooltip" data-placement="left" title="导入当前">
                <i class="fa fa-external-link-square" aria-hidden="true"></i>
            </button>
            <button type="button" id="back" class="btn-raised btn btn-success btn-floating " data-toggle="tooltip"
                    data-placement="left" title="返回">
                <i class="fa fa-mail-reply" aria-hidden="true"></i>
            </button>
        </div>
    </div>
</div>

<div style="display:none;">
    <div id="dlgContent">
        <div class="bolt-form-item">
            <label class="bolt-form-label">包名<span class="required">*</span></label>
            <div class="bolt-input-block">
                <input type="text" realName="basePackage" id="basePackage" class="form-control input-sm">
            </div>
        </div>
        <div class="bolt-form-item">
            <label class="bolt-form-label">模块<span class="required">*</span></label>
            <div class="bolt-input-block">
                <@bolt.select id="moduleId" itemValue="method:loader=menuService"></@bolt.select>
           </div>
        <div class="bolt-form-item">
            <label class="bolt-form-label">生成路径<span class="required">*</span></label>
            <div class="bolt-input-block">
                <input type="text" realName="path" id="path" class="form-control input-sm">
        </div>
    </div>

</div>
</div>
</div>

<link rel="stylesheet" href="${base}/assets/plugins/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="${base}/assets/plugins/codemirror/theme/seti.css">
<link rel="stylesheet" href="${base}/assets/plugins/codemirror/addon/fold/foldgutter.css"/>
<link rel="stylesheet" href="${base}/assets/plugins/bootstrap-fileinput/css/fileinput.min.css"/>
<link rel="stylesheet" href="${base}/assets/css/customizerform.css">

<script src="${base}/assets/plugins/codemirror/lib/codemirror.js" data-realName="codemirror"></script>
<script type="text/javascript" data-deps="codemirror">
    $.namespace('Page.CodeGenerator');
    Page.CodeGenerator = function () {
        return {
            editor: null,
            codeSource: {},//entityCode,controllerCode,repositoryCode
            currentTable: "",
            currentType: "entityCode",
            className: "",
            init: function () {
                setTimeout(function () {
                    Page.CodeGenerator.createCodeEditer()
                    Page.CodeGenerator.loadTableInfo()
                    Page.CodeGenerator.crateAction();
                }, 600)
            },
            loadTableInfo: function (v) {
                Bolt.ajax('doajax', {
                    url: '${base}/admin/sys/code_generator/getAllTables',
                    loadingMask: true,
                    okCallback: function (datas) {
                        var html = template('allTables', {allTables: datas});
                        $('#allTable_info').empty().html(html);
                    }
                })
            },
            createCodeEditer: function () {
                var height = document.documentElement.clientHeight - 200 + "px";
                Page.CodeGenerator.editor = CodeMirror.fromTextArea(document.getElementById("code"), {
                    mode: 'text/x-java',
                    lineNumbers: true,
                    lint: true,
                    lineWrapping: true,
                    tabSize: 2,
                    cursorHeight: 0.9,
                    theme: "seti"	//设置主题
                });
                Page.CodeGenerator.editor.setSize('auto', height);
            },
            crateAction: function () {
                $('.site-action-toggle').on('click', function () {
                    $but = $(this);
                    $('.site-action-buttons').animate({
                        opacity: "toggle",
                        height: "toggle"
                    }, 200);
                    $but.find('i').toggle();
                })

                $('#import').on('click', function () {
                    Page.CodeGenerator.import('all');
                })

                $('#import_current').on('click', function () {
                    Page.CodeGenerator.import('current');
                })
                $('#back').on('click', function () {
                    Bolt.navtab('switchTab', 'main')
                })
            },
            changeEidtor: function (type) {
                Page.CodeGenerator.codeSource[Page.CodeGenerator.currentType] = Page.CodeGenerator.editor.getValue();
                Page.CodeGenerator.editor.setValue(Page.CodeGenerator.codeSource[type]);
                Page.CodeGenerator.currentType = type;
            },
            import: function (type) {
                var dlgContentHTML = $('#dlgContent').html();

                alertify.confirm(dlgContentHTML)
                    .set('title', "确认导入")
                    .set('onok', function (closeevent, value) {
                        var basePackage = $('.ajs-content').find('#basePackage').val();
                        var moduleId = $('.ajs-content').find('#moduleId').val();
                        var path = $('.ajs-content').find('#path').val();
                        if (basePackage == '' || value == "请输入包名称" || basePackage.indexOf(".") <= 0) {
                            alertify.error("请输入合法的包名称");
                            return null;
                        }
                        if (Page.CodeGenerator.className == '') {
                            alertify.error("请选择相应的表");
                            return null;
                        }
                        Page.CodeGenerator.doImport(basePackage, moduleId, type,path);
                    })


            },
            doImport: function (basePackage, moduleId, type,path) {
                var sourceData = {},
                    currentType = Page.CodeGenerator.currentType;
                if ('all' == type) {
                    sourceData = {
                        'basePackage': basePackage, 'moduleId': moduleId,"path":path,
                        "className": Page.CodeGenerator.className,
                        "entityCode": Page.CodeGenerator.codeSource['entityCode'],
                        "controllerCode": Page.CodeGenerator.codeSource['controllerCode'],
                        "repositoryCode": Page.CodeGenerator.codeSource['repositoryCode'],
                        "serviceCode": Page.CodeGenerator.codeSource['serviceCode'],
                        "dtoCode": Page.CodeGenerator.codeSource['dtoCode'],
                        "converterCode": Page.CodeGenerator.codeSource['converterCode']
                    }
                } else {
                    sourceData = {currentType: Page.CodeGenerator.codeSource[currentType]}
                }
                Bolt.ajax('doajax', {
                    data: sourceData,
                    url: '${base}/admin/sys/code_generator/importSource',
                    loadingMask: true,
                    okCallback: function (json) {
                        Bolt.notify(json[Bolt.keys.statusCode], json[Bolt.keys.message]);
                    }
                })
            },
            entityGenerator: function (table) {
                Bolt.ajax('doajax', {
                    data: {'tableName': table},
                    url: '${base}/admin/sys/code_generator/codeGenerator',
                    loadingMask: true,
                    okCallback: function (content) {
                        Page.CodeGenerator.codeSource['entityCode'] = content['entityCode'];
                        Page.CodeGenerator.codeSource['controllerCode'] = content['controllerCode'];
                        Page.CodeGenerator.codeSource['repositoryCode'] = content['repositoryCode'];
                        Page.CodeGenerator.codeSource['serviceCode'] = content['serviceCode'];
                        Page.CodeGenerator.codeSource['dtoCode'] = content['dtoCode'];
                        Page.CodeGenerator.codeSource['converterCode'] = content['converterCode'];
                        Page.CodeGenerator.className = content['className'];
                        Page.CodeGenerator.editor.setValue(Page.CodeGenerator.codeSource['entityCode']);
                        Page.CodeGenerator.currentType = 'entityCode';
                    }
                })
            }

        }
    }();
    $(document).ready(function () {
        Page.CodeGenerator.init();
    });
</script>


